home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
neuralwa
/
nw2v50
/
stockio.c
< prev
next >
Wrap
Text File
|
1993-08-23
|
20KB
|
754 lines
/* stock.c */
/************************************************************************
* Copyright(C) 1987-1992 NeuralWare Inc *
* Penn Center West, IV-227, Pittsburgh, PA 15276 *
* Telephone: (412) 787-8222 FAX: (412) 787-8220 *
* *
* All rights reserved. No part of this program may be reproduced, *
* stored in a retrieval system, or transmitted, in any form or by any *
* means, electronic, mechanical, photocopying, recording or otherwise *
* without the prior written permission of the copyright owner, *
* NeuralWare, Inc. *
* *
* PROPRIETARY NOTICE *
* *
* This document is the property of NeuralWare, Inc. and contains *
* trade-secrets and other proprietary information. The information *
* herein is reserved as proprietary to NeuralWare, and is not to be *
* published, reproduced, copied, disclosed, used, or reverse *
* engineered without the express written consent of a duly authorized *
* representative of NeuralWare. *
************************************************************************
*/
#define UIO_SERVER 1
#define SERVER_EMULATOR_FILE 1
#define SKIP_COMPAT_DATA 1
#include "userutl.h"
#include <math.h>
#include "uio_strc.h"
#include "uio_tran.h"
#include "uio_reqs.pro"
#include "uio_cmds.pro"
#ifdef MAC
#include "macuio.redef"
#endif
/* */
/************************************************************************
* *
* Window management *
* *
************************************************************************
*/
static NINT *null_ptr = 0;
#define W0 1 /* main window */
#define CHT 16 /* character height */
int W0_Wid = {480}; /* basic defining parameters */
int W0_Ht = {200}; /* height of graph */
int W0_Sep = { 50}; /* separation */
int W0_Pix = { 1}; /* pixel size */
#define MX_ALL (10) /* x-for all messages */
#define MY_1 (MX_ALL+10+(W0_Sep*1)/2) /* y-for messages */
#define MY_2 (MX_ALL+10+(W0_Sep*3)/2)
#define MY_3 (MX_ALL+10+(W0_Sep*5)/2)
#define EX0 MX_ALL
#define EX1 (W0_Wid-MX_ALL)
#define EY0 (MX_ALL)
#define EY1 (W0_Ht-MX_ALL)
#define EHT (W0_Ht-2*MX_ALL)
/* */
/************************************************************************
* *
* SetParms() - set up basic display size parameters *
* *
************************************************************************
*/
void SetParms()
{
NINT xsize, ysize, ncolor, chrx, chry; /* graphic parms */
NINT ifx, ify; /* integer factor */
ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry );
ifx = (REAL)xsize / (REAL)W0_Wid;
ify = (REAL)ysize / (REAL)W0_Ht;
if ( ifx > 1 ) {
W0_Wid *= ifx;
}
if ( ify > 1 ) {
W0_Ht *= ify;
W0_Sep *= ify;
}
if ( ifx > 1 && ify > 1 ) {
W0_Pix = 2;
}
return;
}
/* */
/************************************************************************
* *
* PlotArray() - plot a window full of data *
* *
************************************************************************
*/
#ifdef MAC
#define MAIN_COLOR 2
#define AUX_COLOR 5
#else
#define MAIN_COLOR 6
#define AUX_COLOR 13
#endif
typedef struct _WinBlk {
NINT Win; /* window handle */
TEXT *Title; /* title */
NINT X0,Y0; /* coordinates of origin */
NINT X1,Y1; /* coordinates of top right */
NINT XOff; /* x-offset */
NINT XInc; /* x-increment */
REAL Scale, Offset; /* scaling */
NINT px, py; /* previous x,y */
NINT ArraySize; /* array size */
NINT cwx; /* current work index */
SREAL *SavArray; /* save array */
TEXT *SavFlag; /* save flags */
} WB;
VOID PlotSet ARGLIST ((WB *, NINT, TEXT *,
NINT, NINT, NINT, NINT, NINT, NINT,
REAL, REAL, SREAL *, TEXT *, NINT ));
VOID PlotClear ARGLIST (( WB * ));
VOID PlotArray ARGLIST (( WB *, SREAL *, SREAL *, NINT, NINT ));
VOID PlotSave ARGLIST (( WB * ));
VOID PlotNext ARGLIST (( WB *, REAL, REAL, NINT, NINT ));
VOID PlotSet( WBp, Win, Title,
X0,Y0, X1,Y1, XOff, XInc,
Scale, Offset, SaveA, SaveF, NSave )
WB *WBp; /* window block pointer */
NINT Win;
TEXT *Title;
NINT X0,Y0, X1,Y1;
NINT XOff;
NINT XInc;
REAL Scale, Offset;
SREAL *SaveA;
TEXT *SaveF;
NINT NSave;
{
WBp->Win = Win;
WBp->Title = Title;
WBp->X0 = X0;
WBp->Y0 = Y0;
WBp->X1 = X1;
WBp->Y1 = Y1;
WBp->XOff = XOff;
WBp->XInc = XInc;
WBp->Scale = Scale;
WBp->Offset = Offset;
WBp->px = X0 - XInc + (XOff * XInc);
WBp->py = Y0;
WBp->cwx = 0;
WBp->ArraySize = NSave;
WBp->SavArray = SaveA;
WBp->SavFlag = SaveF;
}
VOID PlotClear( WBp )
WB *WBp;
{
NINT wx;
WBp->px = WBp->X0 - WBp->XInc + (WBp->XOff * WBp->XInc);
WBp->py = WBp->Y0;
WBp->cwx = 0;
if ( WBp->SavArray != (SREAL *)null_ptr ) {
for( wx = 0; wx < WBp->ArraySize; wx++ ) {
WBp->SavArray[wx] = 0.0;
WBp->SavFlag[wx] = 0;
}
}
/* clear out the background */
ug_boxf( WBp->Win, 0, 0, WBp->X0, WBp->Y0, WBp->X1, WBp->Y1 );
/* outline box */
ug_box( WBp->Win, 8, 0, WBp->X0-1, WBp->Y0-1, WBp->X1+1, WBp->Y1+1, 0 );
return;
}
VOID PlotArray( WBp, MDp, ADp, NPts, off )
WB *WBp; /* window block pointer */
SREAL *MDp; /* main data array */
SREAL *ADp; /* aux data array */
NINT NPts; /* # of points in the array */
NINT off; /* offset for multiple plots */
{
NINT cx, cy; /* current x,y point */
NINT px, py; /* previous x,y point */
NINT ay; /* y-coordinate of alternate data */
NINT nx; /* index */
NINT lcolor; /* line color */
if ( off == 0 ) {
/* clear out the background */
ug_boxf( WBp->Win, 0, 0, WBp->X0, WBp->Y0, WBp->X1, WBp->Y1 );
/* outline box */
ug_box( WBp->Win, 8, 0, WBp->X0-1,WBp->Y0-1,WBp->X1+1,WBp->Y1+1, 0 );
}
/* plot the data */
px = WBp->X0 - WBp->XInc + WBp->XOff * WBp->XInc;
py = px;
lcolor = ADp == (SREAL *)null_ptr?MAIN_COLOR:AUX_COLOR;
for( nx = 0; nx < NPts && px < WBp->X1; nx++ ) {
/* next x-location */
cx = px + WBp->XInc;
/* y-coordinate & clip it */
cy = off+ WBp->Y0 + ((MDp[nx] + WBp->Offset) * WBp->Scale);
if ( cy < WBp->Y0 ) cy = WBp->Y0;
if ( cy > WBp->Y1 ) cy = WBp->Y1;
if ( cx > WBp->X1 ) cx = WBp->X1;
if ( nx != 0 ) {
ug_line( WBp->Win, lcolor, 0, px, py, cx, cy, 0 );
}
/* auxiliary data */
if ( ADp != (SREAL *)null_ptr ) {
ay = off+ WBp->Y0 + ((ADp[nx] + WBp->Offset) * WBp->Scale);
if ( ay < WBp->Y0 ) ay = WBp->Y0;
if ( ay > WBp->Y1 ) ay = WBp->Y1;
if ( W0_Pix > 1 )
ug_boxf(WBp->Win, MAIN_COLOR, 0,
cx-(W0_Pix-1), ay-(W0_Pix-1),
cx+(W0_Pix-1), ay+(W0_Pix-1) );
else
ug_point( WBp->Win, MAIN_COLOR, 0, cx, ay );
}
/* track the last point */
px = cx;
py = cy;
}
if ( WBp->Title != (char *)null_ptr )
ug_puts( WBp->Win, 15, 0, WBp->X0+5, WBp->Y1-15, WBp->Title, 0 );
return;
}
VOID PlotSave( WBp )
WB *WBp;
{
NINT n;
n = WBp->cwx;
if ( WBp->SavFlag[WBp->ArraySize-1] != 0 )
n = WBp->ArraySize;
if ( WBp->Scale > 0.0 )
PlotArray( WBp, WBp->SavArray, (SREAL *)null_ptr, n, 0 );
}
VOID PlotNext( WBp, MV, AV, af, cf ) /* plot next point */
WB *WBp; /* window block pointer */
REAL MV; /* main value */
REAL AV; /* auxiliary value */
NINT af; /* aux data flag */
NINT cf; /* cursor flag */
{
NINT ay;
NINT cx, cy;
if ( cf != 0 && WBp->Scale > 0.0 ) {
if ( WBp->X0 <= WBp->px && WBp->px < WBp->X1 ) {
ug_line( WBp->Win, 0, 0, WBp->px+1,WBp->Y0,WBp->px+1,WBp->Y1, 0 );
}
}
cx = WBp->px + WBp->XInc;
cy = WBp->Y0 + (MV + WBp->Offset) * WBp->Scale;
if ( cy < WBp->Y0 ) cy = WBp->Y0;
if ( cy > WBp->Y1 ) cy = WBp->Y1;
if ( cx > WBp->X1 ) cx = WBp->X1;
if ( WBp->px < cx && WBp->Scale > 0.0 &&
WBp->px >= (WBp->X0 + WBp->XOff * WBp->XInc) ) {
ug_line( WBp->Win, MAIN_COLOR, 0, WBp->px, WBp->py, cx, cy, 0 );
if ( af ) {
ay = WBp->Y0 + (AV + WBp->Offset) * WBp->Scale;
if ( ay < WBp->Y0 ) ay = WBp->Y0;
if ( ay > WBp->Y1 ) ay = WBp->Y1;
ug_point( WBp->Win, MAIN_COLOR, 0, cx, ay );
}
}
if ( cx >= WBp->X1 ) cx = WBp->X0; /* wrap */
if ( WBp->SavArray != (SREAL *)null_ptr ) {
WBp->SavArray[WBp->cwx] = MV;
WBp->SavFlag[WBp->cwx] = 1;
WBp->cwx++;
if ( WBp->cwx >= WBp->ArraySize || cx == WBp->X0 ) {
WBp->cwx = 0; /* force wrap */
cx = WBp->X0;
}
}
WBp->px = cx;
WBp->py = cy;
if ( cf != 0 && WBp->Scale > 0.0 && WBp->px < WBp->X1 ) {
ug_line( WBp->Win, 15, 0, WBp->px+1, WBp->Y0, WBp->px+1, WBp->Y1, 0 );
}
return;
}
/* */
/************************************************************************
* *
* Common data & statics *
* *
************************************************************************
*/
/*
There are three windows:
top - 4-week moving average w/actual data too
mid - neural network
bot - original data
*/
WB MWB = {0}; /* main */
/* data set definitions */
#define NTPTS 491 /* number of pts in data file */
#define SetSize 450 /* size of training set */
SREAL TrainSet[NTPTS+1] = {0.0}; /* current training set */
SREAL NetPred[NTPTS+1] = {0.0}; /* network prediction */
SREAL TenPred[NTPTS+1] = {0.0}; /* moving average prediction */
NINT InputLen = 0; /* input length */
NINT TSetL = 0; /* training set length */
NINT DataCtr = 0; /* data counter */
SREAL LastDR = 0.0; /* last desired result */
NINT xlag = 14; /* first x-coordinate for network out */
NINT HitRate = 0; /* # of successful times it predicts trend*/
NINT HitTen = 0; /* 10-week hit rate */
NINT TotalCt = 0; /* Total # of recalls */
SREAL PrevWk = 0.0; /* previous week output */
SREAL PrevPrd = 0.0; /* previously predicted output */
SREAL CurTen = 0.0; /* current 10-wk moving average */
SREAL PrevTen = 0.0; /* previous 10-wk moving average */
REAL maxe = 0.0; /* max error (%) */
NINT maxect = 0; /* max error count */
REAL maxe2 = 0.0; /* max error > 20% */
NINT maxe2ct = 0; /* max error 2 count */
REAL TotErr = 0.0; /* total error over all cases */
/* */
/************************************************************************
* *
* Training Set Input / Check it is there Routines *
* *
************************************************************************
*/
NINT IsTSOk()
{
if ( TSetL <= 0 )
return(UIO_ERROR);
else
return(UIO_OK);
}
NINT ReadTS( buf )
TEXT *buf; /* work buffer */
{
FILE *fp; /* input file pointer */
TEXT *sp; /* input string */
NINT c; /* work character */
NINT wx; /* work index */
TSetL = 0;
for(;;) {
PutStr ("What is the training input File (<RETURN> for default) ?");
sp = GetStr();
while( *sp && *sp <= ' ' ) sp++; /* skip leading white space */
if ( *sp == '\0' ) {
sp = "sp500.in"; /* default string */
}
fp = fopen (sp, "r");
if (fp == (FILE *) 0) {
sprintf( buf, "Could not open <%s>\n", sp );
PutStr( buf );
continue;
}
for ( wx = 0; wx < NTPTS;) {
if ( fgets( &buf[0], 80, fp ) == 0 ) break;
/* kill any comments */
for( sp = &buf[0]; (c = *sp) != '\0'; sp++ ) {
if ( c == '!' || c == '*' || c == '\r' || c == '\n' )
break;
}
*sp = '\0'; /* kill comments, etc */
/* skip leading space and check for blank lines */
for( sp = &buf[0]; *sp && *sp <= ' '; ) sp++;
if ( *sp == '\0' ) continue;
sscanf( buf, "%f", &TrainSet[wx] );
wx++;
}
TSetL = wx;
fclose(fp);
if ( TSetL == 0 ) {
PutStr( "Nothing in data set, try again\n" );
continue;
}
break;
}
return( 0 );
}
/*******************************************************************/
/* */
/* Functions necessary for handling the User I/O package itself. */
/* */
/*******************************************************************/
TEXT buf[100], *sp;
NINT UIO_Init(file)
TEXT *file;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Term(process)
NINT process;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Attention()
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Learn_Start()
{
if ( TSetL <= 0 )
ReadTS( &buf[0] ); /* read data */
return ( IsTSOk() );
}
NINT UIO_Learn_Input(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT wx, wn;
NINT ret_val;
if ( (ret_val=IsTSOk()) == UIO_OK) {
if ( nPEs > InputLen )
InputLen = nPEs;
/* pick next training input randomly */
wn = rand() % (TSetL - nPEs - 1);
for (wx = 0; wx < nPEs; wx++, wn++)
Datap[wx] = TrainSet[wn];
/*desired response*/
LastDR = TrainSet[wn];
}
return(ret_val);
}
NINT UIO_Learn_Output(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
Datap[0] = LastDR;
return(UIO_OK);
}
NINT UIO_Learn_Result(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Learn_End()
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Recall_Start()
{
NINT ret_val;
/* open all windows at this time */
SetParms();
ug_window( W0, 0, 80, 3, 80+W0_Wid, 3+W0_Ht );
PlotSet( &MWB, W0, (char *)null_ptr, EX0,EY0,EX1,EY1,
0, (NINT)(3.4*(EX1-EX0)/NTPTS), EHT / 2.1, 0.0,
(SREAL *)null_ptr, (TEXT *)null_ptr, 0 );
if ( TSetL <= 0 )
ReadTS( &buf[0] ); /* read data */
if ((ret_val=IsTSOk()) != UIO_OK)
return(ret_val);
DataCtr = 0;
maxe = 0.0;
maxect = 0;
maxe2 = 0.0;
maxe2ct = 0;
TotErr = 0.0;
ug_winclr( 0 );
xlag = 0;
HitRate = 0;
HitTen = 0;
TotalCt = 0;
return(ret_val);
}
NINT UIO_Read(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT wx, wn;
NINT ret_val;
if ((ret_val=IsTSOk()) == UIO_OK) {
if ( nPEs > InputLen )
InputLen = nPEs;
/* select the next string from the data set */
PrevTen = CurTen;
CurTen = 0.0;
for (wn = DataCtr, wx = 0; wx < nPEs; wx++, wn++ ) {
CurTen += TrainSet[wn];
Datap[wx] = TrainSet[wn];
}
TenPred[wn] =
(1.0 * CurTen) / InputLen; /* save moving average prediction */
PrevWk = LastDR; /* save previous week */
LastDR = TrainSet[wn];
}
return(ret_val);
}
NINT UIO_Write(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
SREAL v;
NINT ret_val = UIO_OK;
NetPred[DataCtr+InputLen] = Datap[0]; /* current prediction */
if ( xlag == 0 ) {
xlag = InputLen;
MWB.XOff = xlag;
}
DataCtr++;
/* see if it correctly predicted up/down trend */
if ( fabs(Datap[0]) > .001 )
v = fabs((Datap[0]-LastDR)/Datap[0]);
else
v = 0.0;
if ( v > .10 ) {
if ( v > maxe2 )
maxe2 = v;
maxe2ct++;
} else {
if ( v > maxe )
maxe = v;
maxect++;
}
TotErr += v;
if ( DataCtr > 1 ) {
SREAL act, prd, ten;
act = LastDR - PrevWk;
prd = Datap[0] - PrevPrd;
ten = CurTen - PrevTen;
/* classify network hit rate */
if ( (fabs(act) < .002 && fabs(prd) < .002) ||
(act < 0.0 && prd < 0.0) || (act > 0.0 && prd > 0.0) )
HitRate++;
if ( (fabs(act) < .002 && fabs(ten) < .002) ||
(act < 0.0 && ten < 0.0) || (act > 0.0 && ten > 0.0) )
HitTen++;
TotalCt++;
}
PrevPrd = Datap[0];
PrevWk = LastDR;
if(DataCtr >= (TSetL - InputLen-1)) {
ret_val = UIO_ERROR;
}
return(ret_val);
}
NINT UIO_Write_Step(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Recall_Test(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
Datap[0] = LastDR;
return(UIO_OK);
}
NINT UIO_Recall_End()
{
NINT ret_val = UIO_OK;
if ( TotalCt == 0 )
TotalCt = 1;
MWB.XOff = 0;
PlotClear( &MWB );
PlotArray( &MWB, &TrainSet[0], (SREAL *)null_ptr, TSetL, 0 );
MWB.XOff = InputLen;
PlotArray( &MWB, &NetPred[InputLen], &TrainSet[InputLen],
TSetL-InputLen-1, W0_Sep*1 );
PlotArray( &MWB, &TenPred[InputLen], &TrainSet[InputLen],
TSetL-InputLen-1, W0_Sep*2 );
/* put up titles */
sprintf( buf, "%ld-Week Moving Average", (SL) InputLen );
ug_puts( W0, 13, 0, MX_ALL+5, MY_1, "S&P 500", 0 );
ug_puts( W0, 13, 0, MX_ALL+5, MY_2, "Network Prediction", 0 );
ug_puts( W0, 13, 0, MX_ALL+5, MY_3, buf, 0 );
DataCtr = 0;
sprintf( buf,
"Trends: Net: %ld/%ld (%ld%%), Ave Err=%.3f%%; %ld-Week: %ld/%ld (%ld%%)",
(SL)HitRate, (SL)TotalCt,
(SL) ((HitRate * 100 + TotalCt/2) / TotalCt),
TotErr / TotalCt * 100.,
(SL) InputLen,
(SL) HitTen, (SL) TotalCt,
(SL) ((HitTen * 100. + TotalCt/2) / TotalCt ));
PutStr( buf );
GetStr();
return(ret_val);
}
NINT UIO_Instrument(Instrument_id, nDataElems, DataElemp)
NINT Instrument_id;
NINT nDataElems;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_ObjFunc(eoeflag, DataElemp)
NINT eoeflag;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Rewind()
{
NINT ret_val = UIO_OK;
return(ret_val);
}
NINT UIO_Explain(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
return(ret_val);
}